import { _decorator, Component, easing, EventTouch, Input, Node, tween, UITransform, Vec2, Vec3 } from 'cc';
import { BaseCard, CardType } from './BaseCard';
import { GameScene } from './GameScene';
import { Tools } from '../Tools';
import { CatCardLoad } from './CatCardLoad';
import { CatCard } from './CatCard';
import { CardLoad } from './CardLoad';
import { DisCard } from './DisCard';
import { UIMgr } from '../Mgr/UIMgr';
import { AssetList } from '../Global';
import { LocalStorageMgr } from '../Mgr/LocalStorageMgr';
const { ccclass, property } = _decorator;

@ccclass('BattleCard')
export class BattleCard extends BaseCard {

    //卡牌
    @property(Node)
    private Card: Node = null;

    //是否碰到了这张牌
    private IsTouchStart: boolean = false;

    protected start(): void {
        this.node.on(Input.EventType.TOUCH_START, this.TouchCb, this);
        this.node.on(Input.EventType.TOUCH_MOVE, this.TouchCb, this);
        this.node.on(Input.EventType.TOUCH_END, this.TouchCb, this);
        this.node.on(Input.EventType.TOUCH_CANCEL, this.TouchCb, this);
    }

    /**
     * 手指监听
     */
    private TouchCb(_e: EventTouch) {
        switch (_e.type) {
            case Input.EventType.TOUCH_START:
                this.IsTouchStart = true;
                break;
            case Input.EventType.TOUCH_MOVE:
                if (!this.IsTouchStart) return;

                let pos: any = _e.getUILocation();
                pos = this.node.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(pos.x, pos.y, 0));
                this.Card.setPosition(pos);

                break;
            case Input.EventType.TOUCH_CANCEL:
                this.TouchEnd();
                break;

            case Input.EventType.TOUCH_END:
                this.TouchEnd();
                break;
        }
    }

    /**
     * 手指抬起
     */
    public TouchEnd() {

        //判断是否碰到了弃牌堆
        if (Tools.CheckHit(this.Card, GameScene.Instance.DisCard) && GameScene.Instance.DisCard.active) {
            GameScene.Instance.DisCard.getComponent(DisCard).UpdateCard(this.CardInfo);
            //执行弃牌堆逻辑，卡牌消失
            GameScene.Instance.CardLoad.getComponent(CardLoad).CardDisappear(this.node);
            this.Card.setPosition(new Vec3(0, 0, 0));
            return;
        }

        //先判断是否碰到了猫咪牌
        for (let cat of GameScene.Instance.CatCardLoad.getComponent(CatCardLoad).CatCardArray) {
            if (Tools.CheckHit(this.Card, cat) && cat.active) {

                if (cat.getComponent(CatCard).HeartNum == 1 && this.CardInfo.num == 0
                    && this.CardInfo.color == cat.getComponent(CatCard).CardInfo.color) {
                    cat.getComponent(CatCard).UpdateHeartNum(this.CardInfo);
                } else {
                    cat.getComponent(CatCard).UpdateHeartNum(this.CardInfo);
                    GameScene.Instance.CardLoad.getComponent(CardLoad).CardDisappear(this.node);
                }
                //执行攻击逻辑，卡牌消失
                this.Card.setPosition(new Vec3(0, 0, 0));

                

                return;
            }
        }


        this.Card.setPosition(new Vec3(0, 0, 0));
    }

    /**
     * 播放发牌动画
     */
    public PlayGiveCardAni(_index: number) {
        this.Card.active = false;

        this.Card.setWorldPosition(GameScene.Instance.CardNum.node.parent.getWorldPosition());

        tween(this.Card).
            delay(_index * 0.1).
            call(() => {
                this.Card.active = true;
            }).
            to(0.2, { position: new Vec3(0, 0, 0) }, { easing: easing.backOut }).
            start();

    }

}


